package cn.lmxdev.ptms.controller;

import cn.lmxdev.ptms.bean.table.TStudentSubmit;
import cn.lmxdev.ptms.bean.table.TUser;
import cn.lmxdev.ptms.bean.vo.ResponseVo;
import cn.lmxdev.ptms.service.IStudentSubmitServlet;
import cn.lmxdev.ptms.service.IUserServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController(value = "studentSubmitController")
@RequestMapping(value = "/api/student-submit")
public class StudentSubmitController {
    private final IStudentSubmitServlet studentSubmitServlet;
    private final IUserServlet userServlet;

    @Autowired
    public StudentSubmitController(
            IStudentSubmitServlet studentSubmitServlet,
            IUserServlet userServlet
    ) {
        this.studentSubmitServlet = studentSubmitServlet;
        this.userServlet = userServlet;
    }

    @GetMapping(value = "/file/{id}")
    public ResponseEntity<ByteArrayResource> file(@PathVariable(value = "id") Long id) {
        TStudentSubmit studentSubmit = this.studentSubmitServlet.get(id);
        ByteArrayResource bar = new ByteArrayResource(studentSubmit.getFile());
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header("Content-disposition", "attachment; filename=" + studentSubmit.getFileName())
                .body(bar);
    }

    @GetMapping(value = "/list")
    public ResponseVo<Object> list(
            @RequestParam(value = "limit", required = false) Long limit,
            @RequestParam(value = "page", required = false) Long page,
            TStudentSubmit studentSubmit
    ) {
        // 准备数据
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        TUser nowUser = this.userServlet.getByAccount(auth.getName());

        // 响应数据变量
        long count = 0L;
        Object data = null;

        // 校验角色
        switch (auth.getAuthorities().toString()) {
            case "[ROLE_ADMIN]" -> {
                count = this.studentSubmitServlet.count(studentSubmit);
                data = this.studentSubmitServlet.list(limit, page, studentSubmit);
            }
            case "[ROLE_DEPARTMENT-HEAD]" -> {
            }
            case "[ROLE_SPECIALTY-HEAD]" -> {
            }
            case "[ROLE_ADVISER]" -> {
                count = this.studentSubmitServlet.adviserCount(nowUser.getId(), studentSubmit);
                data = this.studentSubmitServlet.adviserList(nowUser.getId(), limit, page, studentSubmit);
            }
            case "[ROLE_TEACHER]" -> {
            }
            case "[ROLE_STUDENT]" -> {
                count = this.studentSubmitServlet.count(studentSubmit.setStudentId(nowUser.getId()));
                data = this.studentSubmitServlet.list(limit, page, studentSubmit.setStudentId(nowUser.getId()));
            }
        }

        // 响应数据
        return new ResponseVo<>(count, data);
    }

    @PostMapping(value = "/add")
    public ResponseVo<Object> add(TStudentSubmit studentSubmit, @RequestParam(value = "upFile", required = false) MultipartFile upFile) throws IOException {
        if (upFile != null && !upFile.isEmpty()) {
            studentSubmit.setFileName(upFile.getOriginalFilename());
            studentSubmit.setFileContentType(upFile.getContentType());
            studentSubmit.setFile(upFile.getBytes());
        }
        this.studentSubmitServlet.add(studentSubmit);
        return new ResponseVo<>().setCode(0).setMsg("操作成功");
    }

    @DeleteMapping(value = "/delete/{id}")
    public ResponseVo<Object> delete(@PathVariable(value = "id") Long id) {
        this.studentSubmitServlet.remove(id);
        return new ResponseVo<>().setCode(0).setMsg("操作成功");
    }

    @PutMapping(value = "/edit")
    public ResponseVo<Object> edit(TStudentSubmit studentSubmit) {
        this.studentSubmitServlet.edit(studentSubmit);
        return new ResponseVo<>().setCode(0).setMsg("操作成功");
    }

    @GetMapping(value = "/look/{id}")
    public ResponseVo<Object> look(@PathVariable(value = "id") Long id) {
        return new ResponseVo<>(this.studentSubmitServlet.get(id));
    }
}
